package com.woniuxy.student.controller;

import com.woniuxy.commons.entity.QuestionAnswer;
import com.woniuxy.commons.entity.QuestionDetail;
import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.entity.User;
import com.woniuxy.commons.entity.wyh.TeacherInfo;
import com.woniuxy.student.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: lishuwei
 * @Date: 2020/7/18
 * @description:
 */
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @GetMapping("/getInterlocutors")
//    @PathVariable("id") int id
    public ResponseEntity<Object> getInterlocutors(){
        //获取当前用户，判断用户角色，用学生（老师）ID查询问答记录

        ResponseEntity<Object> responseEntity=new ResponseEntity<>();
        try {
           //查询学生（老师）的老师（学生）们，在其中附上最近的一条消息；
            //如果用户是学生
            List<TeacherInfo> interlocutors=questionService.getTeacherByStuid(1);
//            for (int i=0;i<interlocutors.size();i++){
//                List<QuestionAnswer> questionAnswers=questionService.getQuestionsById(1,interlocutors.get(i).getTcId());
//                TeacherInfo teacherInfo=interlocutors.get(i);
//                teacherInfo.setQuestionAnswers(questionAnswers);
//                interlocutors.set(i,teacherInfo);
//            }
            responseEntity.setCode(200);
            responseEntity.setData(interlocutors);
            return responseEntity;
        }catch (Exception e){
            e.printStackTrace();
            responseEntity.setCode(400);
            responseEntity.setMsg("服务器维护中请稍后");
        }
        return responseEntity;
    }


    @GetMapping("/getQuestions/{id}")
//    @PathVariable("id") int id
    public ResponseEntity<List<QuestionAnswer>> getQuestions(@PathVariable("id")int id){
        //获取当前用户，判断用户角色，用学生（老师）ID查询问答记录

        ResponseEntity< List<QuestionAnswer>> responseEntity=new ResponseEntity<>();
        try {
            List<QuestionAnswer> questionAnswers=questionService.getQuestionsById(1,id);//假设是1学生在查
            Collections.reverse(questionAnswers);
            responseEntity.setCode(200);
            responseEntity.setData(questionAnswers);
            return responseEntity;
        }catch (Exception e){
            e.printStackTrace();
            responseEntity.setCode(400);
            responseEntity.setMsg("服务器维护中请稍后");
        }
       return responseEntity;
    }
 
    @GetMapping("/getDetails/{qaid}")
    public ResponseEntity<List<QuestionDetail>> getDetailByQid(@PathVariable("qaid") int qaid){

        ResponseEntity< List<QuestionDetail>> responseEntity=new ResponseEntity<>();
        try {
            List<QuestionDetail> questionDetails=questionService.getDetailsByQaid(qaid);
            responseEntity.setCode(200);
            responseEntity.setData(questionDetails);
            return responseEntity;
        }catch (Exception e){
            e.printStackTrace();
            responseEntity.setCode(400);
            responseEntity.setMsg("服务器维护中请稍后");
        }
        return responseEntity;

    }
    @PostMapping("/addDetail/")
    public ResponseEntity<Integer> addDetail(QuestionDetail detail){

        ResponseEntity<Integer> responseEntity=new ResponseEntity<>();
        try {

            responseEntity.setCode(200);
            responseEntity.setData(questionService.addDetail(detail));
            return responseEntity;
        }catch (Exception e){
            e.printStackTrace();
            responseEntity.setCode(400);
            responseEntity.setMsg("服务器维护中请稍后");
        }
        return responseEntity;

    }

}
